package net.ys.controller;

import net.yx.service.MinIoService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author nmy
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    @Resource
    MinIoService minIoService;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping
    public String upload(MultipartFile file) {

        String respUrl = null;
        try {
//            String str = "<![CDATA[<html><head><title>hello</title></head><body>hello</body></html>]]>";
            String str = "aaaaaaaaaaa";
            InputStream stream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
            respUrl = minIoService.upload(stream, "test", file.getOriginalFilename(), file.getContentType());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return respUrl;
    }

    @DeleteMapping
    public String delete() {
        minIoService.delete("test", "rebuild.sh");
        return "success";
    }
}
